/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.clientpackets;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.ClientThread;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_CharAmount;
import l1j.server.server.serverpackets.S_CharInfo;

public class C_CommonClick {
	private static final String _C__0B_COMMONCLICK = "[C] C_CommonClick";

	private static Logger _log = Logger.getLogger(C_CommonClick.class.getName());

	private final ClientThread _client;

	public C_CommonClick(ClientThread client) throws Exception
	{
		_client = client;
		byte nochars = AmountofChars(client.getAccountName());
		client.sendPacket(new S_CharAmount(nochars));
		if(nochars > 0)
			CharPack(client.getAccountName(), client);
	}

	public byte AmountofChars(String Account)
	{
		byte amount = 0;

		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try
		{

			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM characters WHERE account_name=? ORDER BY objid");
			pstm.setString(1, Account);

			rs = pstm.executeQuery();
			while(rs.next()) 
				amount++;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally {
			if (null != rs) {
				try {
					rs.close();
				} catch (SQLException ignore) {
					// ignore
				}
				rs = null;
			}

			if (null != pstm) {
				try {
					pstm.close();
				} catch (SQLException ignore) {
					// ignore
				}
				pstm = null;
			}

			if (null != con) {
				try {
					con.close();
				} catch (SQLException ignore) {
					// ignore
				}
				con = null;
			}
		}
		return amount;
	}

	public void CharPack(String Account, ClientThread client) {

		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {

			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM characters WHERE account_name=?");
			pstm.setString(1, Account);
			if (Account == client.getAccountName())
			{
				rs = pstm.executeQuery();

				while(rs.next())
				{
					L1PcInstance pc = new L1PcInstance();
					
					// Get Character Attribute
					String name 	= rs.getString("char_name");
					String clanname = rs.getString("Clanname");
					byte type	 	= rs.getByte("Type");
					byte sex 		= rs.getByte("Sex");
					short lawful 	= rs.getShort("Lawful");
					short currenthp = rs.getShort("CurHp");
					short currentmp = rs.getShort("CurMp");
					byte level 		= rs.getByte("level");
					
					int ac = rs.getByte("Ac");
					int str = rs.getByte("Str");
					int dex = rs.getByte("Dex");
					int Con = rs.getByte("Con");
					int wis = rs.getByte("Wis");
					int cha = rs.getByte("Cha");
					int intel = rs.getByte("Intel");
					
					if(currenthp < 1)
						currenthp = 1;
					else if(currenthp > 32767)
						currenthp = 32767;

					if (currentmp < 1)
						currentmp = 1;
					else if (currentmp > 32767)
						currentmp = 32767;
						
					if(level < 1)
						level = 1;
					else if(level > 127)
						level = 127;
						
					// New Character Info
					S_CharInfo info = new S_CharInfo();
					
					// Character Attribute Load
					info.CharName 	= name;
					info.ClanName 	= clanname;
					info.CharType 	= type;
					info.CharSex	= sex;
					info.CharLawful	= lawful;
					info.CurrentHp	= currenthp;
					info.CurrentMp	= currentmp;
					info.CharAc		= (byte)pc.getAc();
					info.CharLevel	= level;
					info.CharSTR	= pc.getStr();
					info.CharDEX	= pc.getDex();
					info.CharCON	= pc.getCon();
					info.CharCHA	= pc.getCha();
					info.CharWIS	= pc.getWis();
					info.CharINT	= pc.getInt();
					
					// Send Character Info
					client.sendPacket(info);
				}

			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (null != rs)
			{
				try
				{
					rs.close();
				}
				catch(SQLException ignore)
				{
				}
				rs = null;
			}

			if(null != pstm)
			{
				try
				{
					pstm.close();
				}
				catch(SQLException ignore)
				{
				}
				pstm = null;
			}

			if(null != con)
			{
				try
				{
					con.close();
				}
				catch(SQLException ignore)
				{
				}
				con = null;
			}
		}
	}

	public String getType() {
		return "[C] C_CommonClick";
	}
}